from datetime import timedelta

from flask import Flask
from flask_jwt_extended import JWTManager

from routes import register_routes  # 导入路由注册函数
from models import db  # 导入模型
from flask_migrate import Migrate
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True)# 允许所有域名访问
app.config['SECRET_KEY'] = '512451231236456asdasdas56a4d'
app.config['JWT_SECRET_KEY'] = '97*/a84615423123qw232132-)_(_)asdasd'
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(hours=1)  # 设置过期时间
# 设置刷新JWT过期时间
app.config["JWT_REFRESH_TOKEN_EXPIRES"] = timedelta(days=30)
# 配置数据库
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123321@localhost/AIkefu'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SESSION_COOKIE_SECURE'] = True  # 如果是 HTTPS，需要这个设置

# 初始化数据库
db.init_app(app)
jwt = JWTManager(app)
# 初始化迁移
migrate = Migrate(app, db)

# 注册路由
register_routes(app)

# 创建数据库表
with app.app_context():
    db.create_all()  # 创建所有模型对应的表


if __name__ == '__main__':
    app.run(debug=True, port=5000)